package com.example.aicloud.controller;

import com.alibaba.cloud.ai.tongyi.chat.TongYiChatClient;
import com.alibaba.cloud.ai.tongyi.chat.TongYiChatOptions;
import com.alibaba.cloud.ai.tongyi.image.TongYiImagesClient;
import com.alibaba.cloud.ai.tongyi.image.TongYiImagesOptions;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.aicloud.common.ModelType;
import com.example.aicloud.common.Models;
import com.example.aicloud.entity.Answer;
import com.example.aicloud.service.IAnswerService;
import com.example.aicloud.utils.Result;
import com.example.aicloud.utils.SecurityUtil;
import jakarta.annotation.Resource;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.image.ImagePrompt;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author Mr.Tang
 * @date 2024-07-20
 */
@RestController
@RequestMapping("/tongyi")
public class TongyiController {

    @Resource
    private TongYiChatClient chatClient;

    @Resource
    private TongYiImagesClient imagesClient;

    @Resource
    private IAnswerService answerService;

    /**
     * 聊天
     *
     * @param question
     * @return
     */
    @RequestMapping("/chat")
    public Result chat(String question) {
        if (!StringUtils.hasLength(question)) {
            return Result.fail("请输入问题");
        }

        // 调用
//        TongYiChatOptions chatOptions = new TongYiChatOptions();
        String content = chatClient.call(new Prompt(question))
                .getResult()
                .getOutput()
                .getContent();

        // 存入数据库
        Answer answer = new Answer();
        answer.setTitle(question);
        answer.setContent(content);
        answer.setType(ModelType.CHAT.getType());
        answer.setModel(Models.TONGYI.getModel());
        answer.setUid(SecurityUtil.getCurrentUser().getUid());
        boolean save = answerService.save(answer);
        if (!save) {
            return Result.fail("服务器睡着了, 请重试");
        }

        // 返回
        return Result.success(content);
    }


    /**
     * 绘画
     * @param question
     * @return
     */
    @RequestMapping("/draw")
    public Result draw(String question) {
        if (!StringUtils.hasLength(question)) {
            return Result.fail("请输入问题");
        }
        // 调用
        TongYiImagesOptions imagesOptions = new TongYiImagesOptions();
        imagesOptions.setHeight(800);
        imagesOptions.setWidth(800);
        String url = imagesClient.call(new ImagePrompt(question, imagesOptions))
                .getResult().getOutput().getUrl();
        // 保存到数据库
        Answer answer = new Answer();
        answer.setTitle(question);
        answer.setContent(url);
        answer.setType(ModelType.DRAW.getType());
        answer.setModel(Models.TONGYI.getModel());
        answer.setUid(SecurityUtil.getCurrentUser().getUid());
        boolean save = answerService.save(answer);
        if (!save) {
            return Result.fail("服务器睡着了, 请重试");
        }
        // 返回
        return Result.success(url);
    }

    /**
     * 获取聊天历史列表
     * @return
     */
    @RequestMapping("/getchatlist")
    public Result getChatList() {
        long uid = SecurityUtil.getCurrentUser().getUid();
        // 查询历史信息
        QueryWrapper<Answer> wrapper = new QueryWrapper<>();
        wrapper.eq("uid", uid);
        wrapper.eq("type", ModelType.CHAT.getType());
        wrapper.eq("model", Models.TONGYI.getModel());
        wrapper.orderByDesc("aid");
        List<Answer> list = answerService.list(wrapper);
        // 返回
        return Result.success(list);
    }


    /**
     * 获取绘图历史信息
     * @return
     */
    @RequestMapping("/getdrawlist")
    public Result getDrawList() {
        long uid = SecurityUtil.getCurrentUser().getUid();
        // 查询历史信息
        QueryWrapper<Answer> wrapper = new QueryWrapper<>();
        wrapper.eq("uid", uid);
        wrapper.eq("type", ModelType.DRAW.getType());
        wrapper.eq("model", Models.TONGYI.getModel());
        wrapper.orderByDesc("aid");
        List<Answer> list = answerService.list(wrapper);
        // 返回
        return Result.success(list);
    }

}
